package com.magic.smartcity.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.magic.smartcity.R;
import com.magic.smartcity.view.adapters.WelcomeImageAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WelcomePageActivity extends AppCompatActivity {

    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.dots_line)
    LinearLayout dots_line;
    @BindView(R.id.setting_and_go_home)
    LinearLayout setting_and_go_home;
    @BindView(R.id.set_network)
    Button setNetwork;
    @BindView(R.id.go_home)
    Button goHome;

    private int[] images = {R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d,
            R.drawable.e};
    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);//全屏
        getWindow().setStatusBarColor(Color.TRANSPARENT);//状态栏透明
        setContentView(R.layout.activity_welcome_page);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        WelcomeImageAdapter welcomeImageAdapter = new WelcomeImageAdapter();
        mViewPager.setAdapter(welcomeImageAdapter);
        welcomeImageAdapter.setImages(images);
        //初始化引导点
        drawDot(0);
    }

    private void initEvent() {
        //换引导的时候引导点切换
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("TAG", position + ":::::");
                drawDot(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化引导页的引导点
     *
     * @param position
     */
    private void drawDot(int position) {
        dots_line.removeAllViews();
        if (position == images.length - 1) {
            setting_and_go_home.setVisibility(View.VISIBLE);
        } else {
            setting_and_go_home.setVisibility(View.GONE);
        }
        //初始化引导点
        for (int i = 0; i < images.length; i++) {
            LinearLayout dot_linear = new LinearLayout(getApplicationContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40);
            params.setMargins(10, 10, 10, 10);
            dot_linear.setLayoutParams(params);
            dot_linear.setBackground(i == position ? getResources().getDrawable(R.drawable.shape_round_border_selected) : getResources().getDrawable(R.drawable.shape_round_border));
            dots_line.addView(dot_linear);
        }
    }

    @OnClick(R.id.set_network)
    void showSetNetwork() {
        mPopupWindow = new PopupWindow(this);
        Log.d("TAG", "::::点击了设置网络");
    }

    @OnClick(R.id.go_home)
    void goHome() {
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        SharedPreferences.Editor edit = welcome.edit();
        edit.putBoolean("isFirst", false);
        edit.apply();
        startActivity(new Intent(this, MainActivity.class));
    }
}